package yunwo.cn.yzstation.view.fregment.inventory;

import android.app.Dialog;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.andview.refreshview.XRefreshView;

import java.util.List;

import yunwo.cn.yzstation.R;
import yunwo.cn.yzstation.adapter.IvetoryLvadatoer;
import yunwo.cn.yzstation.adapter.Standardadapter;
import yunwo.cn.yzstation.api.Iswitch;
import yunwo.cn.yzstation.bean.InventoryBean;
import yunwo.cn.yzstation.presenter.AllFragmentPersenter;
import yunwo.cn.yzstation.api.IallFragmentView;
import yunwo.cn.yzstation.utils.NetWorkUtils;
import yunwo.cn.yzstation.utils.sharePUtil;
import yunwo.cn.yzstation.view.activity.InventoryActivity;


/**
 * Created by lao on 2017/8/23.
 */

public class AllFragment extends Fragment implements IallFragmentView ,Iswitch{
    private View mRootview ;
    private ListView mListview;
    private XRefreshView mXrefreshview;
//    private RelativeLayout mInventory_title;
    private InventoryActivity mActivity;
    private AllFragmentPersenter mAllFragmentPersenter;
    private IvetoryLvadatoer mIvetoryLvadatoer;
    private Standardadapter mStandardadapter;
    int cout = 1;
    private List<InventoryBean.DataBean.ListBean> mDatalist;
    InventoryBean.DataBean mDataBean;
    boolean IsSearch = false;
    private LinearLayout mZhuangtai;
    private Dialog mMyDialog;
    private TextView mZhuangtaiText;
    private ImageView mZhuangtaiIMG;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        mRootview = inflater.inflate(R.layout.fragment_inventory, container, false);
        if (mMyDialog == null) {
            mMyDialog = ProgressDialog.show(getContext(), null, "请稍等...");
            mMyDialog.setCancelable(false);
            mMyDialog.show();
        }
        return mRootview;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        mActivity = (InventoryActivity) getActivity();
        mActivity.getIswitch(this);
        mAllFragmentPersenter = new AllFragmentPersenter(mActivity,this,this);
        initData();
        initView();
    }

    private void initData() {
        if (!NetWorkUtils.isNetworkConnected(getContext())) {
            if (mZhuangtai != null) {
                mZhuangtai.setVisibility(View.VISIBLE);
                mZhuangtaiIMG.setImageResource(R.drawable.icon_no_network);
                mZhuangtaiText.setText("网络出错,请检查您的网络~");
                mListview.setVisibility(View.GONE);
                mXrefreshview.setVisibility(View.GONE);
            }
        }else {
            if (mZhuangtai != null) {
                mZhuangtai.setVisibility(View.GONE);
            }
        }

        mAllFragmentPersenter.getData("",1,5);
    }

    @Override
    public void onResume() {
        super.onResume();
        initData();
        boolean state = sharePUtil.getInventorySP(getContext()).getBoolean("state", true);

        if (!state) {
            //// TODO: 2017/8/31   列表适配器

//                mInventory_title.setVisibility(View.VISIBLE);


            mStandardadapter.refreshDataAndNotifyDataSetChanged(mDatalist);
            mListview.setAdapter(mStandardadapter);
        }else {
//            mInventory_title.setVisibility(View.GONE);

            mIvetoryLvadatoer.refreshDataAndNotifyDataSetChanged(mDatalist);
            mListview.setAdapter(mIvetoryLvadatoer);
        }
    }

    private void initView() {

        mListview = (ListView) mRootview.findViewById(R.id.inventory_lv);
        mXrefreshview = (XRefreshView) mRootview.findViewById(R.id.xrefreshview);

        mZhuangtai = (LinearLayout) mRootview.findViewById(R.id.zhuangtai);
        mZhuangtaiText = (TextView) mRootview.findViewById(R.id.zhuangtaiText);
        mZhuangtaiIMG = (ImageView) mRootview.findViewById(R.id.zhuangtaiIMG);
        mIvetoryLvadatoer = new IvetoryLvadatoer(getContext());

        mIvetoryLvadatoer.refreshDataAndNotifyDataSetChanged(mDatalist);
        mListview.setAdapter(mIvetoryLvadatoer);
        mXrefreshview.setPullLoadEnable(true);
        mXrefreshview.setPullRefreshEnable(true);
        mXrefreshview.setAutoLoadMore(false);
        mXrefreshview.setScrollBackDuration(300);
        mStandardadapter = new Standardadapter(getContext());
        mXrefreshview.setXRefreshViewListener(new XRefreshView.SimpleXRefreshListener(){
            @Override
            public void onRefresh(boolean isPullDown) {

                mAllFragmentPersenter.getData("1",1,5);

            }

            @Override
            public void onLoadMore(boolean isSilence) {
                if (mDataBean!=null &&mDataBean.isHasNextPage()) {
                    cout += 1;
                    mAllFragmentPersenter.getData("2",cout,5);
                    mXrefreshview.stopLoadMore();
                }else {
                    Toast.makeText(getContext(), "已加载全部数据", Toast.LENGTH_SHORT).show();
                    mXrefreshview.stopLoadMore(false);
                }
            /*    new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        //模拟数据加载失败的情况
                        Random random = new Random();
                        boolean success = random.nextBoolean();
                        if (success) {
                            Toast.makeText(getContext(),"下拉刷新成功", Toast.LENGTH_SHORT).show();
                            mXrefreshview.stopLoadMore();
                        } else {
                            Toast.makeText(getContext(),"失败", Toast.LENGTH_SHORT).show();
                            mXrefreshview.stopLoadMore(false);

                        }

                    }
                }, 2000);*/
            }
        });
    }

    @Override
    public void getListDataSuccess(InventoryBean.DataBean dataBean,String type) {
        this.mDataBean = dataBean;
        if (mMyDialog != null && mMyDialog.isShowing()) {
            mMyDialog.dismiss();
        }

        if (dataBean.getList().size() == 0) {

            if (mZhuangtai != null) {
                mZhuangtai.setVisibility(View.VISIBLE);
            }
        }else {
            if (mZhuangtai != null) {
                mZhuangtai.setVisibility(View.GONE);
            }
        }

        switch (type) {
            case "":
                mDatalist = dataBean.getList();
                mIvetoryLvadatoer.refreshDataAndNotifyDataSetChanged(dataBean.getList());
                mStandardadapter.refreshDataAndNotifyDataSetChanged(dataBean.getList());
                break;
            case "1":
                if (mXrefreshview != null) {
                    mXrefreshview.stopRefresh();
                    cout = 1;
                    if (dataBean.getList().size() == 0) {
                        Toast.makeText(getContext(),"暂无数据",Toast.LENGTH_SHORT).show();

                    }else {
                        Toast.makeText(getContext(), "刷新成功", Toast.LENGTH_SHORT).show();
                    }
                    mDatalist = dataBean.getList();
                    mStandardadapter.refreshDataAndNotifyDataSetChanged(dataBean.getList());
                    mIvetoryLvadatoer.refreshDataAndNotifyDataSetChanged(dataBean.getList());
                }
                break;
            case "2":
                if (mXrefreshview != null) {
                    mXrefreshview.stopLoadMore();
                    Toast.makeText(getContext(), "加载成功", Toast.LENGTH_SHORT).show();
                    mDatalist.addAll(dataBean.getList());
                    mIvetoryLvadatoer.addDataAndNotifyDataSetChanged(dataBean.getList());
                    mStandardadapter.addDataAndNotifyDataSetChanged(dataBean.getList());
                }
                break;
        }
    }

    @Override
    public void getListDataFailure() {
        if (mMyDialog != null && mMyDialog.isShowing()) {
            mMyDialog.dismiss();
        }
        if (mZhuangtai != null) {
            mZhuangtai.setVisibility(View.VISIBLE);
            mZhuangtaiIMG.setImageResource(R.drawable.icon_no_network);
            mZhuangtaiText.setText("网络开小差了");
            mListview.setVisibility(View.GONE);
        }
    }

    @Override
    public void switc(boolean state) {
        if (!state) {

            //// TODO: 2017/8/31   列表适配器
        /*    if (mDatalist.size() > 0) {
                mInventory_title.setVisibility(View.VISIBLE);
            }*/

            mStandardadapter.refreshDataAndNotifyDataSetChanged(mDatalist);
            mListview.setAdapter(mStandardadapter);
        }else {
//            mInventory_title.setVisibility(View.GONE);
            mIvetoryLvadatoer.refreshDataAndNotifyDataSetChanged(mDatalist);
            mListview.setAdapter(mIvetoryLvadatoer);
        }
    }
}
